package org.example.stockdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.stockdemo.entity.StockDetail;
import org.example.stockdemo.entity.StockIndustryRank;
import org.example.stockdemo.entity.StockRank;
import org.example.stockdemo.mapper.StockDetailMapper;
import org.example.stockdemo.mapper.StockRankMapper;
import org.example.stockdemo.service.StockDetailService;
import org.example.stockdemo.service.StockRankService;
import org.example.stockdemo.util.BeanExtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class StockRankServiceImpl extends ServiceImpl<StockRankMapper, StockRank> implements StockRankService {

    @Transactional
    @Override
    public boolean batchSave(List<StockRank> stockRanks) {
        if (CollectionUtils.isNotEmpty(stockRanks)) {
            LocalDate now = LocalDate.now();
            LocalDateTime dateTime = LocalDateTime.now();

            // 构建查询条件：code, sortType, createdAt
            List<String> codes = stockRanks.stream()
                    .map(StockRank::getCode)
                    .collect(Collectors.toList());
            String sortType = stockRanks.get(0).getSortType();
            LambdaQueryWrapper<StockRank> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(StockRank::getCode, codes)
                    .eq(StockRank::getSortType, sortType)
                    .eq(StockRank::getCreatedAt, now);
                    
            List<String> existingCodeResult = list(queryWrapper).stream()
                    .map(StockRank::getCode).collect(Collectors.toList());;

            List<StockRank> stockRankCpList = BeanExtUtil.copyList(stockRanks, StockRank.class);

            // 分离需要更新和需要保存的记录
            List<StockRank> upDetailList=new ArrayList<>();
            Iterator<StockRank> iterator = stockRankCpList.iterator();
            while (iterator.hasNext()){
                StockRank stockRank = iterator.next();
                stockRank.setCreatedAt(now);
                stockRank.setUpdatedAt(dateTime);
                if (existingCodeResult.contains(stockRank.getCode())) {
                    upDetailList.add(stockRank);
                    iterator.remove();
                }
            }

            // 批量更新
            if (CollectionUtils.isNotEmpty(upDetailList)){
                upDetailList.stream().forEach(stockRank -> {
                    UpdateWrapper<StockRank> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("code", stockRank.getCode())
                            .eq("sort_type", stockRank.getSortType())
                            .eq("created_at", stockRank.getCreatedAt());
                    update(stockRank, updateWrapper);
                });
            }

            // 批量插入
            if (CollectionUtils.isNotEmpty(stockRankCpList)){
                return saveBatch(stockRankCpList);
            }
        }
        return false;
    }

}